﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;

namespace PlaylistParitySL
{
    public class WMPUtilities
    {
        public static ObservableCollection<Playlist> GetPlaylists()
        {
            ObservableCollection<Playlist> returnList = new ObservableCollection<Playlist>();

            if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) + @"\Playlists"))
            {
                return returnList;
            }
            IEnumerable<string> files = Directory.EnumerateFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) + @"\Playlists");

            foreach (string file in files)
            {
                StreamReader reader = new StreamReader(file);

                var doc = XDocument.Load(reader);

                var titleNode = doc.Root.Descendants("head").Descendants("title");

                Playlist wmpPlaylist = new Playlist();
                wmpPlaylist.Name = titleNode.First().Value;

                var songList = doc.Root.Descendants("body").Descendants("media");

                foreach (XElement element in songList)
                {
                    Song song = new Song();
                    song.Location = element.Attribute("src").Value;
                    song.Name = element.Attribute("src").Value;

                    wmpPlaylist.SongList.Add(song);
                }
                returnList.Add(wmpPlaylist);
            }

            return returnList;
        }
    }
}
